package jdk.lambda.collectors;

import jdk.entity.UserEntity;
import jdk.entity.UserEntityList;
import java.time.LocalDate;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;

/**
 * Collectors.mapping 的样例
 * 适用场景： 分组后在对集合做特殊处理 如 toList ,toCount,toAvg,toFloatMap
 * @author songyafei
 */
public class MappingTest {
    public static void main(String[] args) {
        List<UserEntity> item = UserEntityList.getItem();


        /**
         * 场景：提取所有的用户名称并组装为 [,,,] 格式
         * 实现：Collectors.joining(",","[","]")
         */
        String s = item.stream().collect(Collectors.mapping(m-> m.getName(),Collectors.joining(",","[","]")));
        System.out.println(s);


        /**
         * 场景： 获取相同日期下的用户id
         * 实现 根据日期分组，value = 用户id 集合
         * 扩展： 再改场景的基础加上去重，只需要把最后的toList 改为 toSet
         */
        Map<LocalDate, List<Long>> map = item.parallelStream().collect(Collectors.groupingBy(UserEntity::getCreatedDate,
                Collectors.mapping(m -> m.getUserId(), Collectors.toList())));
        System.out.println(map.toString());


        Map<LocalDate, List<Boolean>> M1 = item.parallelStream().collect(Collectors.groupingBy(m -> m.getCreatedDate(),
                Collectors.mapping(m -> m.getAge() > 18, Collectors.toList())));

    }
}
